﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
    using System.Data.SqlClient;

namespace Domain.Ef
{
    using Domain.Accounts;
    using ECommon.Components;
    using Infrastructure;

    [Component]
    public class AccountIndexRepository : IAccountIndexRepository
    {
        public AccountIndex FindByAccountName(string accountName)
        {
            using (Repository.EntityFramework.Accounts.DataContext db = new Repository.EntityFramework.Accounts.DataContext())
            {
                var m = db.AccountIndex.AsNoTracking().Where(x => x.AccountName == accountName).SingleOrDefault();
                if (m != null)
                {
                    return new AccountIndex(m.AccountId, m.AccountName);
                }
            }
            //using (var connection = GetConnection())
            //{
            //    connection.Open();
            //    var data = connection.QueryList(new { AccountName = accountName }, "AccountIndex" ).SingleOrDefault();
            //    if (data != null)
            //    {
            //        return new AccountIndex(data.AccountId as string, accountName);
            //    }
            //}


            return null;
        }
        public void Add(AccountIndex index)
        {
            using (Repository.EntityFramework.Accounts.DataContext db = new Repository.EntityFramework.Accounts.DataContext())
            {
                db.AccountIndex.Add(new Repository.EntityFramework.Accounts.AccountIndex { AccountId = index.AccountId, AccountName = index.AccountName });
                db.SaveChanges();
            }
            //using (var connection = GetConnection())
            //{
            //    connection.Open();
            //    connection.Insert(new
            //    {
            //        AccountId = index.AccountId,
            //        AccountName = index.AccountName
            //    }, "AccountIndex");
            //}
        }

        //private SqlConnection GetConnection()
        //{
        //    return new SqlConnection(ConfigSettings.ConnectionString);
        //}
    }
}
